<?php
include_once (MODEL.'/Repository.php');
include_once (MODEL . '/OfferModel.php');
class OfferRepository extends Repository
{
    public $collection;
  
	public function __construct()
	{
		parent::__construct();				
                $this->collection=$this->DB->selectCollection('Offer');
	}
	
    public function GetListCategory() 
    {
		$this->collection = $this->DB->selectCollection('Category');
		return $this->collection->find();
    }
    public function GetListOffer()
    {
    	$list = $this->DB->selectCollection('Offer')->find()->limit(6);
    	$listOffer  =  array();
    	while($list->hasNext())
    	{
    		$offer = new OfferModel();
    		$result = $list->getNext();
    		$offer->ID  = $result['_id'];
    		$offer->Image = $result['Image'];
    		$offer->Name = $result['Name'];
    		$offer->Description = $result['Description'];
    		$listOffer[] = $offer;
    	}
    	return $listOffer;
    }
	
    public function LastestStuff()
    {
    	$cursor = $this->collection->find()->limit(10);
    	$listOffer = array();
    	
    	while($cursor->hasNext())
    	{
    		$offer = new OfferModel();
    		$result = $cursor->getNext();
    		$offer->ID = $result['_id'];
    		$offer->Title = $result['Name'];
    		$offer->Image = $result['Image'];
    		$listOffer[] = $offer;
    	}
    	return $listOffer;
    }
        
               
    
    // Tính tổng số collection có ID category = $categoryID
    public function TotalByCategory($categoryID)
    {
    	if ($categoryID !='') $filter = array('CategoryID'=>new MongoId($categoryID)); 
        else $filter = array();
        return $this->collection->find($filter)->count();
    }
    
    // Trả về mảng các Offer có ID category =$categoryID của trang
    public function ViewListOfferByCategory($categoryID, $start, $limit)
    {
       
        if ($categoryID !='') $filter = array('CategoryID'=>new MongoId($categoryID)); 
        else $filter = array();
        $cursor = $this->collection->find($filter)->skip($start)->limit($limit); 
        $listOffer = array();
        
		while($cursor->hasNext()) 
		{
			$offer = new OfferModel();
			$result = $cursor->getNext();
			$offer->ID = $result['_id'];
			$offer->Name = $result['Name'];
			$offer->Location = $result['Location'];
			$offer->Image = $result['Image'];
			
			$user = $this->DB->selectCollection('Account')->findOne(array('_id' => new MongoId($result['AccountID'])));
			$offer->OwnerID = $user['_id'];
			$offer->OwnerName = $user['FirstName'];
						
			$listOffer[] = $offer;
		}
		
		return $listOffer;
    }
    
    public function CreateOffer($model) 
    {
    	//$this->collection = $this->DB->selectCollection('Offer');
    	                                               
       	$location = '';
        $location .= $model->Street;
        $location .= ', ' . $model->City;
        $location .= ', ' . $model->Province;
        
        $data = array(
            'Name'  => $model->Name,
            'CategoryID' =>new MongoId( $model->CategoryID),
            'AccountID' => $model->AccountID,
            'Location'  => $location,
            'CreateDate'  => new MongoDate(),
            'Image'  => 'gift.jpg',
            'ActiveDate' => '',
            'Description'  => $model->Description,
        		'Views' => '7',
        		'isActive' => 'True',
        		'isDelete' => 'False',
        		'isPublished' => 'True',
       );                           
       
       //print_r($data);
       $this->collection->insert($data);       
       return $data['_id'];                                                                
    }
    public function DeleteOffer($filter)
    {
        $this->collection->remove($filter);
    }
    public function GetDetail($offerID)
    {
        include_once (MODEL . '/OfferModel.php');
        
        $filter = array('_id'=> new MongoId($offerID));
        $result = $this->collection->findOne($filter);
        $offer = new OfferModel();
		$offer->ID = $result['_id'];
		$offer->Name = $result['Name'];
        $Category = $this->DB->selectCollection('Category')->findOne(array('_id' => new MongoId($result['CategoryID'])));
        $offer->Category = $Category['Name'];
		$offer->Location = $result['Location'];
		$offer->Image = $result['Image'];
		$user = $this->DB->selectCollection('Account')->findOne(array('_id' => new MongoId($result['AccountID'])));
		$offer->OwnerID = $user['_id'];
		$offer->OwnerName = $user['FirstName'];
        $offer->Description = $result['Description'];
        return $offer;
    }
    
    public function EditOffer($offID,$offName,$offCategory,$offDescription,$offLocation) 
    {  
    	$image = $offID.".jpg";
       $this->collection->update(array('_id'=>new MongoId($offID)),array('$set'=>array('Name'=>$offName,'Location'=>$offLocation,'CategoryID'=>new mongoID($offCategory),'Description'=>$offDescription,'Image'=>$image)),array("upsert"=>false));    
                                                                     
    }          
}